package com.geovis.emergency.spd.biz.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.system.mapper.SystemRegionMapper;
import com.geovis.emergency.spd.biz.system.service.ISystemRegionService;
import com.geovis.emergency.spd.entity.system.entity.SystemDept;
import com.geovis.emergency.spd.entity.system.entity.SystemRegion;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import com.geovis.emergency.spd.entity.system.pojo.node.TreeNode;
import com.geovis.emergency.spd.entity.system.pojo.vo.SystemRegionVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.geotools.ows.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.geovis.emergency.spd.cache.RegionCache.*;

/**
 * <p>
 * 行政区划表 服务实现类
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/6/15
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SystemRegionServiceImpl  extends ServiceImpl<SystemRegionMapper, SystemRegion> implements ISystemRegionService {
    private final String COMMA = ",";
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public boolean submit(SystemRegion region) {
        Long cnt = baseMapper.selectCount(Wrappers.<SystemRegion>query().lambda().eq(SystemRegion::getCode, region.getCode()));
        if (cnt > 0) {
            return this.updateById(region);
        }
        // 设置祖区划编号
        SystemRegion parent = getByCode(region.getParentCode());
        if (ObjectUtil.isNotEmpty(parent) || StrUtil.isNotEmpty(parent.getCode())) {
            String ancestors = parent.getAncestors() + COMMA + parent.getCode();
            region.setAncestors(ancestors);
        }
        // 设置省、市、区、镇、村
        Integer level = region.getRegionLevel();
        String code = region.getCode();
        String name = region.getName();
        if (level == PROVINCE_LEVEL) {
            region.setProvinceCode(code);
            region.setProvinceName(name);
        } else if (level == CITY_LEVEL) {
            region.setCityCode(code);
            region.setCityName(name);
        } else if (level == DISTRICT_LEVEL) {
            region.setDistrictCode(code);
            region.setDistrictName(name);
        } else if (level == TOWN_LEVEL) {
            region.setTownCode(code);
            region.setTownName(name);
        } else if (level == VILLAGE_LEVEL) {
            region.setVillageCode(code);
            region.setVillageName(name);
        }
        return this.save(region);
    }

    @Override
    public boolean removeRegion(String id) {
        Long cnt = baseMapper.selectCount(Wrappers.<SystemRegion>query().lambda().eq(SystemRegion::getParentCode, id));
        Assert.isFalse(cnt > 0,"请先删除子节点!");
        return removeById(id);
    }

    @Override
    public List<SystemRegionVO> lazyList(String parentCode, Map<String, Object> param) {
        return baseMapper.lazyList(parentCode, param);
    }

    @Override
    public List<TreeNode> lazyTree(String parentCode, Map<String, Object> param) {
        return baseMapper.lazyTree(parentCode, param);
    }

    @Override
    public void removeByIdList(Set<String> idList) {
        if (CollUtil.isEmpty(idList)) {
            return;
        }
        //1、刪除本身
        removeByIds(idList);

        //3、刪除孩子节点
        List<SystemRegion> list = baseMapper.selectList(Wrappers.lambdaQuery(SystemRegion.class)
                .select(SystemRegion::getCode)
                .in(SystemRegion::getParentCode, idList));
        Set<String> childrenIdList = CollUtil.set(false);
        list.forEach(systemRegion -> childrenIdList.add(systemRegion.getCode()));
        removeByIdList(childrenIdList);
    }
}
